/**
 * 对http请求参数代理
 */
import EventBus from '@aecworks/event-bus';
import Axios from 'axios';

const argumentsProxy = (vo) => {
    if (vo) {
        if (typeof vo.revert === 'function') {
            return vo.revert();
        } else {
            return vo;
        }
    } else {
        return null;
    }
};

/**
 * 对http结果代理
 */
const httpResultProxy = (resultPromise, ViewObjectClass = null) => {
    return new Promise((resolve, reject) => {
        resultPromise
            .then(
                (response) => {
                    if (response) {
                        // logger.log({log: 'httpResultProxy: response from bimserver', response: {...response}})
                        if (response.status === 200) {
                            resolve(mapViewObject(ViewObjectClass, response.data));
                        } else {
                            reject(response);
                        }
                    } else {
                        reject(new Error('服务器返回数据异常'));
                    }
                },
                (error) => {
                    // logger.log(error)
                    reject(error);
                }
            )
            .catch((error) => {
                if (error.response) {
                    reject(new Error(error.response.statusText));
                } else {
                    reject(new Error('服务器异常！'));
                }
            });
    });
};

const mapViewObject = (ViewObjectClass, data) => {
    if (ViewObjectClass) {
        if (Array.isArray(data)) {
            let arr = [];
            for (let item of data) {
                arr.push(new ViewObjectClass(item).getModel());
            }
            return arr;
        } else {
            return new ViewObjectClass(data).getModel();
        }
    } else {
        return data;
    }
};
/**
 + * 对http请求前代理
 + */
const interceptors = (axios, isResponseDetail?) => {
    // axios.defaults.timeout = 1000;
    // const log = Logger('proxy', {logLevel: 'debug'});
    axios.interceptors.request.use(
        (config) => {
            EventBus.instance
                .dispatch('SIGN_IN', config)
                .then(() => {
                    // log.debug('请求前success');
                })
                .catch(() => {});
            return config;
        },
        (error) => {
            // log.debug('请求前err');
            return Promise.reject(error);
        }
    );
    axios.interceptors.response.use(
        (response) => {
            // Do something with response data
            return response;
        },
        (error) => {
            EventBus.instance
                .dispatch('RESPONSE_ERROR', error)
                .then(() => {
                    // log.debug('请求前success');
                })
                .catch(() => {});
            if (isResponseDetail) {
                return Promise.reject(error);
            } else {
                if (Axios.isCancel(error)) {
                    return Promise.reject(error.message);
                } else {
                    return Promise.reject(error.response);
                }
            }
        }
    );
};
export {argumentsProxy, httpResultProxy, mapViewObject, interceptors};
